<?php

namespace app\api\controller\v1;

use addons\litestore\model\Litestoregoodsspec;
use addons\litestore\model\Wxlitestoregoods;
use app\admin\model\litestore\Litestoregoods;
use app\admin\model\litestore\Litestorenews;
use app\admin\model\Orderday;
use app\admin\model\Profitorder;
use app\admin\model\Profitorderundo;
use app\admin\model\Profitvip;
use app\admin\model\Usersales;
use app\api\service\ProfitService;
use app\common\controller\Api;
use think\Db;
use think\Exception;

/**
 */
class Sales extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];



    public function index(){

        $user = $this->auth->getUser();
        $uid = $user['id'];

        $userSalesModel = new Usersales();
        $salesUser =  $userSalesModel->where('uid',$uid)->order('createtime','desc')->find();
        $data = array();
        if(!$salesUser){
            $data['month'] = 0;
            $data['all'] = 0;
            $data['teamMonth'] = 0;
            $data['teamAll'] = 0;
        }else{
            $data['month'] = $salesUser['month'];
            $data['all'] =  $salesUser['all'];
            $data['teamMonth'] =  $salesUser['teamMonth'];
            $data['teamAll'] = $salesUser['teamAll'];
        }


        $this->success('',$data);

    }

    public function monthList(){


        $user = $this->auth->getUser();
        $uid = $user['id'];
        $page = $this->request->param('page');
        $limit = 10;
        $offset = ($page - 1) * $limit;
        $where = array();
        $where['uid'] = $uid;
        $orderdayModel = new Orderday();
        $list = $orderdayModel
            ->where($where)
            ->order(['createtime' => 'desc']) ->limit($offset, $limit)
            ->select();
        foreach ($list as $key=>$val){
            $list[$key]['createtime'] = date('Y-m-d',$val['createtime']);
        }
        $this->success('',$list);
    }


    public function teamList(){


        $user = $this->auth->getUser();
        $page = $this->request->param('page');
        $keyword = $this->request->param('keyword');

        $uid =  $user['id'];
        $limit = 10;
        $offset = ($page - 1) * $limit;
        $where = array();
        $where['ref_uid'] = $uid;
//        $startTime = strtotime(date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y"))));
//        $where['sales.createtime'] = $startTime;

        if ($keyword != '') {
            $where['username'] = array('like', '%' . $keyword . '%');
        }

        $userModel = new \app\admin\model\User();
        $list = $userModel->with(['sales'])
            ->where($where)
            ->group('user.id')
            ->order(['sales.all' => 'desc'])->limit($offset, $limit)
            ->select();



        foreach ($list as $key => $val) {
            if ($val['group_id'] == 1) {
                $list[$key]['group_name'] = 'U粉丝';
            } else if ($val['group_id'] == 2) {
                $list[$key]['group_name'] = '推荐官';
            } else if ($val['group_id'] == 3) {
                $list[$key]['group_name'] = '代言人';
            } else if ($val['group_id'] == 4) {
                $list[$key]['group_name'] = '合伙人';
            }
            if (strpos($val['username'], "o") === 0) {
                $list[$key]['username'] = '未实名';
            }
            if($val['sales']['month'] == null){
                $list[$key]['sales']['month'] = 0;
            }
            if($val['sales']['all'] == null){
                $list[$key]['sales']['all'] = 0;
            }
            if($val['sales']['teamAll'] == null){
                $list[$key]['sales']['teamAll'] = 0;
            }
            if($val['sales']['teamMonth'] == null){
                $list[$key]['sales']['teamMonth'] = 0;
            }
        }

        $this->success('',$list);
    }


}
